package com.projectzqjz.parttimework.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.entity.DailyEntity;
import com.projectzqjz.parttimework.utils.ScreenUtil;

import java.util.List;

public class PartApplyAdapter extends BaseQuickAdapter<DailyEntity, BaseViewHolder> {
    private Activity activity;
    private int screenWidth;
    private int intervalWidth;
    private int spec;
    public PartApplyAdapter(Activity activity, List<DailyEntity> newsList) {
        super(R.layout.item_popular_part_time, newsList);
        this.activity = activity;
        screenWidth = ScreenUtil.getScreenWidth(activity);
        intervalWidth = ScreenUtil.dip2px(activity, 40);
        spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    }

    @Override
    protected void convert(BaseViewHolder helper, DailyEntity item) {
        View centerLine=helper.getView(R.id.v_center_line);
        if (item.getGsName().equals("")){
            centerLine.setVisibility(View.GONE);
        }
        LinearLayout llAddress = helper.getView(R.id.ll_address);
        LinearLayout llUnit = helper.getView(R.id.ll_unit);
        TextView tvName = ((TextView) helper.getView(R.id.tv_name));
        View line_v=helper.getView(R.id.line);
        ((TextView)helper.getView(R.id.tv_location)).setText(item.getLocation());
        tvName.setText(item.getGsName());
        ((TextView)helper.getView(R.id.tv_money)).setText(item.getMoney());
        ((TextView)helper.getView(R.id.tv_unit)).setText(item.getUnit());
        TextView title_v = helper.getView(R.id.tv_title);
        TextView label_v = helper.getView(R.id.tv_state);
        title_v.setText(item.getTitle().trim());
        label_v.setText(item.getLabel());
        label_v.measure(spec, spec);
        title_v.measure(spec, spec);
        llAddress.measure(spec, spec);
        llUnit.measure(spec, spec);
        tvName.measure(spec,spec);
        int measuredLabelWidth = label_v.getMeasuredWidth();
        int measuredTitleWidth = title_v.getMeasuredWidth();
        int maxWidth=(screenWidth - intervalWidth - measuredLabelWidth-ScreenUtil.dip2px(activity,7));
        if (measuredTitleWidth >= maxWidth) {
            RelativeLayout.LayoutParams linearParams =(RelativeLayout.LayoutParams) title_v.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
            linearParams.width = maxWidth;// 控件的宽强制设成30
            title_v.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }else {
            RelativeLayout.LayoutParams linearParams =(RelativeLayout.LayoutParams) title_v.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
            linearParams.width = measuredTitleWidth;// 控件的宽强制设成30
            title_v.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }
        //适配公司地址信息
        int gsWidth = (screenWidth - llAddress.getMeasuredWidth() - llUnit.getMeasuredWidth() - intervalWidth - ScreenUtil.dip2px(activity, 8));
        if (tvName.getMeasuredWidth() >= gsWidth) {
            RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) tvName.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20

            linearParams.width = gsWidth;// 控件的宽强制设成30

            tvName.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }else {
            RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) tvName.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20

            linearParams.width = tvName.getMeasuredWidth();// 控件的宽强制设成30

            tvName.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }
    }
}
